<?php

namespace app\model;

use think\Model;

class Order extends Model
{
    const PAY_FAIL = 0;
    const PAY_SUCCESS = 1;

    public static function getOrderInfo($order_id)
    {
        $order = Order::alias('o')
            ->join('course c', 'o.course_id = c.course_id', 'LEFT')
            ->where('o.order_id', $order_id)
            ->field('o.*,c.start_time,c.end_time,c.address as course_address,c.product_start_time,c.server_start_time,c.server_end_time,c.description,c.product_end_time')
            ->find();
        if ($order) {
            $order = $order->toArray();
            $order['member'] = Db('orderMember')->where('order_id', $order_id)->select();
            $time = time();
            if ($order['product_start_time'] && $order['product_start_time'] < $time && $order['product_end_time'] > $time) {
                $order['product'] = Db('product')->where('type',1)->order('product_id asc')->select();
                foreach ($order['product'] as $k => $v) {
                    $order['product'][$k]['product_num'] = floor($v['num'] / 2);
                }
            }
            if ($order['server_start_time'] && $order['server_start_time'] < $time && $order['server_end_time'] > $time) {
                $order['server'] = Db('product')->where('type',2)->order('product_id asc')->select();
                foreach ($order['server'] as $k => $v) {
                    $order['server'][$k]['product_num'] = Db('productOrder')->where('product_id',$v['product_id'])->where('status','gt',0)->count();
                }
            }
        } else {
            $order = [];
        }
        return $order;
    }

    public static function getTransaction()
    {
        do {
            $order = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8) . mt_rand(10, 99);
        } while (Order::where('transaction', $order)->find());
        return $order;
    }

    public static function setStatus($order_id, $status)
    {
        $info = Order::where('order_id', $order_id)->find()->toArray();
        if (Order::where('order_id', $order_id)->setField('status', $status) !== false) {
            if ($status == self::PAY_SUCCESS && $info['status'] == self::PAY_FAIL) {
                Order::where('order_id', $order_id)->setField('pay_time', date('Y-m-d H:i:s'));
                $course = Db('course')->where('course_id',$info['course_id'])->find();
                if($course['medal_id']){
                    if(!Db('userMedal')->where('user_id',$info['user_id'])->where('medal_id',$course['medal_id'])->find()){
                        $medal = Db('medal')->where('medal_id',$course['medal_id'])->find();
                        $data = [
                            'user_id' => $info['user_id'],
                            'medal_id' => $medal['medal_id'],
                            'medal_path' => $medal['image'],
                            'create_time' => $course['end_time'],
                        ];
                        Db('userMedal')->insert($data);
                    }
                }
            }elseif($status == self::PAY_FAIL) {
                Order::where('order_id', $order_id)->setField('pay_time', null);
            }
            return true;
        }
        return false;
    }
}
